<template>
	<view class="body">
		<xk-header :headerbg="true" :back="true" title="待预约看房"></xk-header>
		<view class="top-empty-view"></view>

		<view class="list-info">
			<scroll-view class="data-list" scroll-y="true" @scrolltolower="onScrollToLower">
				<checkbox-group @change="checkboxChange">
					<view class="data-item" v-if="dataList && dataList.length > 0"
						v-for="(item, index) of dataList" :key="index">
						<view  >
							<view class="item-top">
								<checkbox :value="item.property_id" :checked="selectIndexList.indexOf(item.property_id) !== -1"/>
								<view class="title">物业编号: {{item.property_code || ""}}</view>
							</view>
							<view class="item-middel">
								<image v-if="item.property_image" mode="aspectFill" class="img" :src="item.property_image | formatImgPath('260', '195')"/>
								<image v-else mode="aspectFill" class="img" :src="(imgUrl+'/wx/default_img.jpg')"/>
								<view class="info">
									<view>
										<view class="name">{{item.tag_name || "-"}}</view>
										<view class="desc">实用{{item.real_area}}㎡/总面积{{item.floor_area}}㎡</view>
									</view>
									<view>
										<view v-if="item.pricing_amount" class="amount"><text></text>{{(item.pricing_amount * item.floor_area || "") | formatMoneyNo00}} <text>元/月</text></view>
										<view v-else class="amount">月租面议</view>
										<view v-if="item.pricing_amount" class="desc">{{(item.pricing_amount || "") | formatMoneyNo00}}元/㎡</view>
									</view>
								</view>
							</view>
							<view class="location-view">
								<image mode="aspectFill" class="dingwei" :src="(imgUrl+'/wx/dingwei.png')"/>
								<view>
									<view class="address">{{item.building || "-"}}/{{item.storey || "-"}}</view>
								</view>
							</view>
						</view>

						<view class="btn-view" >
							<view class="common-btn btn-sty2 btn" @click="onCancel(item)">删除</view>
						</view>
					</view>
				</checkbox-group>

				<view class="empty-view" v-if="!dataList || dataList <= 0">
					<image class="empty-icon" :src="(imgUrl+'/wx/empty_data_icon.png')"></image>
					<view>哎呀，该界面暂无内容~</view>
				</view>
			</scroll-view>
		</view>

		<checkbox-group @change="allCheckChange">
			<view class="bottom-view">
				<view>
					<view class="left-view">
						<checkbox :checked="isCheckAll" value="all"/>
						<view>全选  共</view>
						<view class="number">{{selectIndexList.length}}</view>
						<view>套</view>
					</view>
					<view class="common-btn btn" @click="onAppt()">立即预约</view>
				</view>
			</view>
			</checkbox-group>
		<ApptTimePicker ref="apptTimePicker" @change="startTimeChange" title="查看物业时间段"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl:this.$config.BASE_imgUrl,
				
				scrollViewHeightBottom: 0,
				scrollViewHeight: 0,

				start: 0,
				isLoad: false,	// 数据加载标识
				isReload: false,	// 重新载入标识
				triggered: false,
				isCheckAll: false,
				dataList: [],
				selectIndexList: [],
				curItem: {},
			};
		},
		onLoad(){
			this.initInfo()
		},
		onShow() {
			this.updateHeight()
		},
		methods:{
			initInfo(){
				this.queryList()
			},
			updateHeight() {
				this.$util.getHeightWithTopEl(".top-empty-view").then(height => {
					this.scrollViewHeight = height - 10;

					if (this.selectIndexList.length > 0) {
						this.$util.getElRect(".bottom-view").then(rect => {
							this.scrollViewHeightBottom = height - rect.height - 10;
						})
					} else {
						this.scrollViewHeightBottom = this.scrollViewHeight
					}

				});
			},
			queryList() {
				// this.triggered = 'restore'
				this.dataList = this.$util.getShoppingCart()
				// this.triggered = false
			},
			allCheckChange(e) {
				if (e.detail.value.length > 0) {	// 全选
					this.selectIndexList = [];
					for (let item of this.dataList) {
						this.selectIndexList.push(item.property_id)
					}
					this.isCheckAll = true;
				} else {
					this.isCheckAll = false;
					this.selectIndexList = [];
				}

				this.updateHeight()
			},
			checkboxChange(e) {
				console.warn("checkboxChange: ", e.detail.value)
				this.selectIndexList = e.detail.value
				if(this.selectIndexList.length == this.dataList.length){
					this.isCheckAll = true;
				}else{
					this.isCheckAll = false;
				}
				this.updateHeight()
			},
			onCancel(item) {
				this.$util.confirm({
					content: "确实要将该物业移出购买车？",
					confirmText: "移除"
				}).then(isConfirm => {
					if (isConfirm) {
						let index = this.selectIndexList.indexOf(item.property_id)
						if(index != -1){
							this.selectIndexList.splice(index, 1)
						}
						this.$util.removeShoppingCart([
							item.property_id
						])
						this.queryList();
					}
				})
			},
			onAppt() {
				if (this.selectIndexList.length <= 0) {
					this.$util.toast("请先选择要预约的物业")
					return;
				}
				let dataListTemp = []
				for(let item of this.dataList){
					if(this.selectIndexList.includes(item.property_id)){
						dataListTemp.push(item)
					}
				}
				let eventName = "shopping-cart-appt";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, item => {
					this.$EventBus.$off(eventName)

					this.selectIndexList = [];

					this.queryList()
				})

				uni.navigateTo({
					url: "/pages/estate/make-appt-house?dataList="+JSON.stringify(dataListTemp)+"&eventName="+eventName
				})
			},
			onScrollToLower(e) {
				if (this.isLoad) return
				this.isLoad = true
				this.triggered = true
				this.queryList()
			},
			onRefresh() {
				console.warn("sdfsdfsdf: ", this.isLoad)
				if (this.isLoad) return
				this.isReload = true
				this.triggered = true
				this.queryList()
			},
		}
	}
</script>

<style lang="scss">

.tabs-bar{
	display: flex;
	align-items: center;
	flex-direction: row;
	justify-content: space-around;
	padding: 20rpx 30rpx 0rpx 30rpx;
	width: 750rpx;
	background-color: #FFFFFF;
	white-space: nowrap;
	.item{
		display: inline-block;
		height: 65rpx;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		margin-right: 60rpx;
		color: #333333;
		border-bottom: 6rpx solid rgba(#2C72FF, 0);
	}
	.item-a {
		font-weight: bolder;
		border-bottom: 6rpx solid #2C72FF;
	}
}

.list-info {
	.data-list{
		border-radius: 10rpx;
		height: calc( 100vh - var(--status-bar-height) - 100rpx - 130rpx);
		.data-item {
			display: flex;
			flex-direction: column;
			margin-bottom: 30rpx;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			margin: 20rpx 30rpx 0rpx 30rpx;
			padding: 20rpx;
			color: #333;
			font-size: 28rpx;
			white-space: normal;
			.item-title {
				display: flex;
				align-items: center;
				font-size: 28rpx;

				.title {
					flex: 1;
				}

				.status-other {
					padding: 5rpx 15rpx;
					font-size: 24rpx;
					color: #2C72FF ;
					background-color: rgba($color: #A5C2FF, $alpha: 0.16);
					border-radius: 5rpx;
				}

				.status {
					color: #FF6400;
					background-color: rgba($color: #FFCDA5, $alpha: 0.16);
				}
			}
			.item-top {
				display: flex;
				align-items: center;
				margin-top: 30rpx;

				>checkbox {
					margin-right: 5rpx;
				}


				.title {
					flex: 1;
				}
			}

			.item-middel {
				display: flex;
				margin-top: 10rpx;

				.img{
					width: 200rpx;
					height: 150rpx;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}
				.info{
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.name{
						font-size: 28rpx;
						font-weight: 600;
						color: #333333;
					}
					.desc{
						font-size: 24rpx;
						color: #999999;
						font-weight: 400;
					}
					.amount{
						font-size: 36rpx;
						color: #FF3737;
						font-weight: 700;
						text{
							font-size: 24rpx;
							color: #FF3737;
							font-weight: 600;
						}
					}
				}
			}
			.location-view {
				display: flex;
				margin-top: 30rpx;
				padding-top: 20rpx;
				align-items: flex-start;
				font-size: 24rpx;
				border-top: 1rpx #eee solid;

				.dingwei {
					width: 30rpx;
					height: 30rpx;
					margin-top: 5rpx;
				}
				.address {
					color: #333;
					margin-left: 10rpx;
				}
				.date1 {
					color: #999;
					margin: 10rpx 0rpx;
				}
				.date2 {
					color: #2C72FF;
				}
			}

			.data-sub-item {
				margin-top: 20rpx;
				padding: 20rpx;
				background-color: #F8F8F8;
			}
			.date-view {
				font-size: 28rpx;
				margin-top: 30rpx;
				margin-bottom: 30rpx;
				:first-child {
					color: #999;
				}
				:last-child {
					color: #2C72FF;
					margin-top: 20rpx;
				}
			}
			.btn-view {
				display: flex;
				align-items: flex-end;
				justify-content:flex-end;

				.btn {
					font-size: 28rpx;
					width: 175rpx;
					height: 60rpx;
					margin: 0rpx 0rpx 10rpx 0rpx;
				}
				.btnNoBg {
					background-color: #FFFFFF;
					border: 1rpx #999999 solid;
					margin-right: 20rpx;
				}

			}
		}
	}
}

.bottom-view {
	position: fixed;
	width: 750rpx;
	bottom: var(--window-bottom);

	>view {
		display: flex;
		flex: 1;
		align-items: center;
		background-color: #FFFFFF;
		padding: 35rpx 30rpx 35rpx 30rpx;

		.left-view {
			display: flex;
			align-items: center;
			flex: 1;
			font-size: 28rpx;
			color: #333;

			.number {
				color: #FF3737;
				margin: 0 10rpx;
				font-weight: bold;
			}
		}
		.btn {
			width: 280rpx;
			height: 80rpx;
			padding: 0rpx;
			margin: 0rpx;
		}
	}
}


</style>
